﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace ZK.FWQPrintTBWindowsService
{
    public partial class Service1 : ServiceBase
    {
        HardWareHelp.MW_102N_CardReader mW_102N_CardReader;
        private System.Timers.Timer timer = new System.Timers.Timer(Convert.ToDouble(Common.AppConfigHelper.GetAppSettings("ReaderTime"))); //设置时间间隔为5秒
        ScalesPrint scalesPrint = new ScalesPrint();
        PrintPreviewDialog printPriview = new PrintPreviewDialog();
        Common.YxYsTransportReturnDto yxYsTransportReturnDto;
        static object obj;
        public Service1()
        {
            InitializeComponent();
            Common.Log4.LogManage.WriteInfo("Service1  初始化");
            try
            {
                obj = new object();
                yxYsTransportReturnDto = new Common.YxYsTransportReturnDto();
                string ip = Common.AppConfigHelper.GetAppSettings("ReaderIp");
                mW_102N_CardReader = new HardWareHelp.MW_102N_CardReader(ip, 6000);
                Common.DelegateHelper.MW_102N_MessageReceivedEvent += MW_102N_MessageReceived;//读卡器读到卡的委托

                timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_TimesUp);
                timer.AutoReset = false; //
                timer.Enabled = true;
                timer.Start();
                Common.Log4.LogManage.WriteInfo("所有初始化成功");
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        private void Timer_TimesUp(object sender, ElapsedEventArgs e)
        {
        }

        private void MW_102N_MessageReceived(string cardNum)
        {

            try
            {
                lock (obj)
                {
                    Common.Log4.LogManage.WriteInfo("当前刷卡的卡号是：" + cardNum);
                    if (timer.Enabled)
                    {
                        //生成榜单接口
                        {
                            var resMakePoundList = Common.WebApiService.TruckWeightBill(
                              new Common.WeightBillDtoSendM()
                              {
                                  cardNo = cardNum,
                                  truckNo = "",
                              }
                              );
                            if (!resMakePoundList.success)
                            {
                                Common.Log4.LogManage.WriteErr("生成榜单接口 " + resMakePoundList.msg);
                            }
                        }
                        //结束运单接口
                        {
                            var resEnd = Common.WebApiService.EndYxInfo(cardNum);
                            if (!resEnd.success)
                            {
                                Common.Log4.LogManage.WriteErr("结束运单接口 " + resEnd.msg);
                            }
                        }
                        //查询最后一条运单接口 拿到数据显示在页面上
                        {
                            var getYxInfoNewRes = Common.WebApiService.GetTheLatest(cardNum);
                            if (!getYxInfoNewRes.success)
                            {
                                Common.Log4.LogManage.WriteErr("查询最后一条运单接口 拿到数据显示在页面上 " + getYxInfoNewRes.msg);
                            }
                            if (getYxInfoNewRes.data == null)
                            {
                                Common.Log4.LogManage.WriteErr("查询最后一条运单接口 拿到数据显示在页面上 数据为空！");
                            }
                            else
                            {
                                yxYsTransportReturnDto = getYxInfoNewRes.data;
                                Common.Log4.LogManage.WriteInfo("当前数据：" + yxYsTransportReturnDto.cargoName);
                                //打印预览
                                scalesPrint.yxYsTransportReturnDto = yxYsTransportReturnDto;
                                Common.Log4.LogManage.WriteInfo("开始打印");
                                printPriview = new PrintPreviewDialog
                                {
                                    Document = scalesPrint.printDocument(ScalesPrint.ScalesType.one),
                                    WindowState = FormWindowState.Maximized,
                                };
                                printPriview.Document.BeginPrint += Document_BeginPrint;
                                printPriview.Document.Print();
                                Common.Log4.LogManage.WriteInfo("打印成功");
                            }
                        }
                        Common.Log4.LogManage.WriteInfo("刷一次卡成功");
                        timer.Start();
                    }
                }

            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }

        }

        private void Document_BeginPrint(object sender, PrintEventArgs e)
        {
            System.Drawing.Printing.PrintDocument printDocument = sender as System.Drawing.Printing.PrintDocument;
            bool printRes = printDocument.PrintController.IsPreview;
            if (!printRes)
            {
                printPriview.Close();
            }
        }

        protected override void OnStart(string[] args)
        {
            Common.Log4.LogManage.WriteInfo("打开服务");


            MW_102N_MessageReceived("12qw3e3e3");
        }

        protected override void OnStop()
        {
            Common.Log4.LogManage.WriteInfo("关闭服务");
        }
    }
}
